19. Central Limit Theorem
Number of Outcomes¶
We have already seen coin flips, and how they relate to Pascal's triangle, how they evolve in to binomial distribution earlier in Problem Set 2 - Probability
section.
In 19A. Central Limit Theorem
section, Sebastian ups the game with tossing a die with 3 outcomes (1,2,3). While we could follow pattern to calculate results, visualizing how that outcomes would be could be helpful to strengthen our insights. So I have created another set of functions that could help us do that.
# just a helper function for easier youtube call
def strip_url(url):
return url.replace('https://youtu.be/','')
from IPython.display import YouTubeVideo
url = 'https://youtu.be/jajxhNBnbmI'
YouTubeVideo(strip_url(url))
What is the largest sum of outcomes possible?
In given problem (die toss resulting in 0 or 1 or 2), the maximum possible outcome is 2.
If we toss 4 times, each time maximum possibility is 2, so, the largest sum we could get out of all sequences is $4 \times 2 = 8$
Counting Outcomes 5¶
url = 'https://youtu.be/4d6upzsvBTk'
YouTubeVideo(strip_url(url))
from graphviz import Digraph
from anyflipviz import draw_graph
# define our problem
n_outcomes = 3 # 0,1,2
n_flips = 1 # as per current problem in above video
g = Digraph(strict=True)
g = draw_graph(g, n_outcomes, n_flips)
g
As can be seen above, for just 1 flip, sum is just the outcome as there is nothing more to add. So X=0 sum 0 occurs 1 time, sum 1 occurs 1 time, and sum 2 occurs 1 time.
$ \text {Let X denote the sum of sequence. Then after 1 toss} \\ X = 0, \ n(X) = 1 \\ X = 1, \ n(X) = 1 \\ X = 2, \ n(X) = 1 $
Counting Outcomes 6¶
The tricky part starts here..
url = 'https://youtu.be/QUw97os4gAc'
YouTubeVideo(strip_url(url))
Instead of following pattern established earlier and used by Sebastian above, let us visualize the problem in its entirety.
# define our problem
n_outcomes = 3 # 0,1,2
n_flips = 2 # as per current problem in above video
g = Digraph(strict=True)
g = draw_graph(g, n_outcomes, n_flips)
g
Now each seqeunce will have 2 digits. For eg, take left most branch in above tree. Its 00. So sum is 0. And then we have 01, Sum = 1 and so on. Let us also tabulate these sequences for your better understanding. Along the way, I also note the sequence's sum X.
import pandas as pd
from anyflipviz import get_combinations
temp_df = get_combinations(n_outcomes, n_flips)
temp_df
Let us now count, the no of occurances of the sums., that is n(X). Assuming equal probability for all 3 outcomes, we could also calculate p(X) alongside n(X). You could count n(X) from above table and cross verify with below table.
from anyflipviz import get_combinations_consolidated
final_df = get_combinations_consolidated(n_outcomes, n_flips)
# JUST TO HIGHLIGHT A COLUMN
# thank you: https://stackoverflow.com/questions/41654949/pandas-style-function-to-hignlight-specific-columns
coldict = {'n(x)':'yellow'}
def highlight_cols(s, coldict):
if s.name in coldict.keys():
return ['background-color: {}'.format(coldict[s.name])] * len(s)
return [''] * len(s)
final_df.style.apply(highlight_cols, coldict=coldict)
Note that, the highlighted numbers exactly match what Sebastian arrives at. While we recognize and use patterns, it is important to also be able to connect back to this foundational emergence of pattern. While visualizing tree, can help you appreciate the complexity of the problem we have simplified in to patterns, the derivation helps to keep connected to the fundamentals.
Counting Outcomes 7¶
url = 'https://youtu.be/b5gHsdQFZ3k'
YouTubeVideo(strip_url(url))
# define our problem
n_outcomes = 3 # 0,1,2
n_flips = 3 # as per current problem in above video
g = Digraph(strict=True)
g = draw_graph(g, n_outcomes, n_flips)
g
Whew, its big. Over to all those possible sequences..
temp_df = get_combinations(n_outcomes, n_flips)
temp_df
For just 3 flips, the problem has outgrown in to a complex one, with so many to count..Thanks to our program, now we could easily calculate the no of occurances of all possible sums.
from anyflipviz import get_combinations_consolidated
final_df = get_combinations_consolidated(n_outcomes, n_flips)
final_df.style.apply(highlight_cols, coldict=coldict)
Whoa! We got it right! :)
Take a moment to compare and wonder how this complicated evolution becomes an easy pattern counting Sebastian deploys, and this could easily be applied to any number of flips without we going through above complicated trees.
Counting Outcomes 8¶
Here we go, for even bigger viz..
url = 'https://youtu.be/zVZJkMwBOwA'
YouTubeVideo(strip_url(url))
# define our problem
n_outcomes = 3 # 0,1,2
n_flips = 4 # as per current problem in above video
g = Digraph(strict=True)
g = draw_graph(g, n_outcomes, n_flips)
g
temp_df = get_combinations(n_outcomes, n_flips)
temp_df
final_df = get_combinations_consolidated(n_outcomes, n_flips)
final_df.style.apply(highlight_cols, coldict=coldict)
Central Limit Theorem¶
url = 'https://youtu.be/RAGZjOOc7o8'
YouTubeVideo(strip_url(url))
There is no question here, I just added, for proper conclusion of the pattern we observed so far. Let us draw as well for n_flips = 4, just to see, how the graph looks. What is to be fathomed is, why the sequences have this property? It has nothing to do with probability (which again as a density function that would happen just as a linear function of this distribution as shown on RHS ) so may be we should dig further, why this phenomenon occurs. When you encounter probability questions, and one says assume guassian distribution, the key to why so, lies in understanding this distribution we observe.
from anyflipviz import plot_combinations_consolidated
plot_combinations_consolidated(final_df, fontsize=12)
LHS is the graph, Sebastian talks about in above video.